package com.transport.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.transport.model.Word;
import com.transport.service.WordService;
import com.transport.utils.BaseAction;

public class WordAction extends BaseAction {
	private WordService wordService;
	private Word word;

	public WordService getWordService() {
		return wordService;
	}

	public void setWordService(WordService wordService) {
		this.wordService = wordService;
	}

	public Word getWord() {
		return word;
	}

	public void setWord(Word word) {
		this.word = word;
	}

	public String queryWordList(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		Map map = wordService.queryWordList(this.getPageSet());
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		String data = gson.toJson(map);
		System.out.println(data);
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String checkExist(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		Map<String, String> map = new HashMap<String, String>();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		String flag = wordService.checkExist(word);
		map.put("status", flag);
		String data = gson.toJson(map);
		System.out.println(data);
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String addWord(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		Map<String, String> map = new HashMap<String, String>();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		String flag = wordService.addWord(word);
		map.put("status", flag);
		String data = gson.toJson(map);
		System.out.println(data);
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void deleteWord(){
		String id = ServletActionContext.getRequest().getParameter("id");
		wordService.deleteWord(id);
	}
	
	public String queryWordById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		word = wordService.queryWordById(id);
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		String data = gson.toJson(word);
		System.out.println(data);
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String updateWord(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		Map<String, String> map = new HashMap<String, String>();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		String flag = wordService.updateWord(word);
		map.put("status", flag);
		String data = gson.toJson(map);
		System.out.println(data);
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
